import {ChangeDetectorRef, Component} from '@angular/core';
import {LoadingController, NavController, NavParams} from 'ionic-angular';
import {BozhuinfoPage} from "../bozhuinfo/bozhuinfo";
import {JidihttpProvider} from "../../providers/jidihttp/jidihttp";

/**
 * Generated class for the BozhulistPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */
@Component({
  selector: 'page-bozhulist',
  templateUrl: 'bozhulist.html',
})
export class BozhulistPage {
  /** bozhu **/
  jidiMoviesbozhu: Array<any>=[];
  /** 列表 基地 **/
  tasklistbozhu:Array<any>=[];//list列表

  /** 分页 **/
  pageno:number=1;
  loading: any;
  infiniteEvent:any;//上拉加载的全局事件

  constructor(public navCtrl: NavController, public navParams: NavParams,public jidiProvider:JidihttpProvider,public cdchange:ChangeDetectorRef,public loadingCtrl:LoadingController) {
    /** 博主请求 **/
    this.bozhuhttpClinet();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad BozhulistPage');
  }
  ///
  bozhuinfoEvent(){
    this.navCtrl.push(BozhuinfoPage);
  }

  /** 博主请求 **/
  bozhuhttpClinet(){
    this.jidiProvider.getBozhuHttp(this.pageno).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.jidiMoviesbozhu.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklistbozhu=this.jidiMoviesbozhu;
        this.cdchange.detectChanges();
      }
    },error => {

    });


  }

  /** 分页请求 **/
  doInfinite(infinite){
    this.loading = this.loadingCtrl.create({
      content: '努力加载中...'
    });
    this.loading.present();


    this.infiniteEvent=infinite;
    this.pageno+=1;
    this.jidiProvider.getBozhuHttp(this.pageno).subscribe(data => {
      if (this.loading) {
        this.loading.dismiss();
      }
      if(this.infiniteEvent){
        if(data['data']['records']['length']<1){
          this.infiniteEvent.enable(false);
        }
        for(let i=0;i<data['data']['records']['length'];i++){
          this.jidiMoviesbozhu.push(data['data']['records'][i]);
        }
        this.tasklistbozhu=this.jidiMoviesbozhu;
        this.infiniteEvent.complete();
        this.cdchange.detectChanges();
      }
    },error => {

    });


  }
}
